
$.inputArea = undefined;
$.outputArea = undefined;

$(function(){
	//powered by zhengkai.blog.csdn.net

	//init input code area
	$.inputArea = CodeMirror.fromTextArea(document.getElementById("inputArea"), {
		mode: "text/x-sql", // SQL
		theme: "idea",  // IDEA主题
		lineNumbers: true,   //显示行号
		smartIndent: true, // 自动缩进
		autoCloseBrackets: true// 自动补全括号
	});
	$.inputArea.setSize('auto','auto');

	// init output code area
	$.outputArea = CodeMirror.fromTextArea(document.getElementById("outputArea"), {
		theme: "idea",   // IDEA主题
		lineNumbers: true,   //显示行号
		smartIndent: true, // 自动缩进
		autoCloseBrackets: true// 自动补全括号
	});
	$.outputArea.setSize('auto','auto');

});


const vm = new Vue({
	el: '#rrapp',
	data: {
		formData: {
			tableSql: " CREATE TABLE `dios_shop_trade`  (\n" +
				"	`id` bigint(20) NOT NULL COMMENT 'ID',\n" +
				"	`create_by` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建者',\n" +
				"	`create_time` datetime(6) NULL DEFAULT NULL COMMENT '创建时间',\n" +
				"	`delete_flag` bit(1) NULL DEFAULT NULL COMMENT '删除标志',\n" +
				"	`update_by` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新者',\n" +
				"	`update_time` datetime(6) NULL DEFAULT NULL COMMENT '更新时间',\n" +
				"	`consignee_address_id_path` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址id',\n" +
				"	`consignee_address_path` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址名称',\n" +
				"	`consignee_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '收货人姓名',\n" +
				"	`discount_price` decimal(10, 2) NULL DEFAULT NULL COMMENT '优惠的金额',\n" +
				"	`flow_price` decimal(10, 2) NULL DEFAULT NULL COMMENT '总价格',\n" +
				"	`freight_price` decimal(10, 2) NULL DEFAULT NULL COMMENT '运费',\n" +
				"	`goods_price` decimal(10, 2) NULL DEFAULT NULL COMMENT '原价',\n" +
				"	`member_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '买家id',\n" +
				"	`member_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '买家用户名',\n" +
				"	`payment_method` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '支付方式',\n" +
				"	`sn` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '交易编号',\n" +
				"	`delivery_method` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '配送方式',\n" +
				"	`consignee_mobile` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '收件人手机',\n" +
				"	`pay_status` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,\n" +
				"	`payment_mode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '支付模式：PAYMENT_AFTER_ARRIVAL-货到付款',\n" +
				"	PRIMARY KEY (`id`) USING BTREE\n" +
				") ENGINE = InnoDB CHARACTER SET = utf8 COMMENT='交易' COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;                                         ",
			options: {
				dataType: "sql",

				authorName: "${(value.author)!!}",
				packageName: "${(value.packageName)!!}",
				returnUtilSuccess: "${(value.returnUtilSuccess)!!}",
				returnUtilFailure: "${(value.returnUtilFailure)!!}",

				isPackageType: true,
				isSwagger: false,
				isAutoImport: false,
				isWithPackage: false,
				isComment: true,
				isLombok: true,

				ignorePrefix:"sys_",
				tinyintTransType: "int",
				nameCaseType: "CamelCase",
				timeTransType: "Date"
			}
		},
		templates:[{}],
		historicalData:[],
		outputStr: "${(value.outputStr)!!}",
		outputJson: {}
	},
	methods: {
		//set the template for output 选择页面输出的模板类型
		setOutputModel: function (event) {
			const targetModel = event.target.innerText.trim();
			console.log(targetModel);
			vm.outputStr=vm.outputJson[targetModel];
			$.outputArea.setValue(vm.outputStr.trim());
			//console.log(vm.outputStr);
			$.outputArea.setSize('auto', 'auto');
		},
		//switch HistoricalData
		switchHistoricalData: function (event) {
			const tableName = event.target.innerText.trim();
			console.log(tableName);
			if (window.sessionStorage){
				const valueSession = sessionStorage.getItem(tableName);
				vm.outputJson = JSON.parse(valueSession);
				console.log(valueSession);
				alert("切换历史记录成功:"+tableName);
			}else{
				alert("浏览器不支持sessionStorage");
			}
			vm.outputStr=vm.outputJson["plusentity"].trim();
			$.outputArea.setValue(vm.outputStr);
			//console.log(vm.outputStr);
			$.outputArea.setSize('auto', 'auto');
		},
		setHistoricalData : function (tableName){
			//add new table only
			if(vm.historicalData.indexOf(tableName)<0){
				vm.historicalData.unshift(tableName);
			}
			//remove last record , if more than N
			if(vm.historicalData.length>9){
				vm.historicalData.splice(9,1);
			}
			//get and set to session data
			const valueSession = sessionStorage.getItem(tableName);
			//remove if exists
			if(valueSession!==undefined && valueSession!=null){
				sessionStorage.removeItem(tableName);
			}
			//set data to session
			sessionStorage.setItem(tableName,JSON.stringify(vm.outputJson));
			//console.log(vm.historicalData);
		},
		//request with formData to generate the code 根据参数生成代码
		generate : function(){
			//get value from codemirror
			vm.formData.tableSql=$.inputArea.getValue();
			axios.post("code/generate",vm.formData).then(function(res){
				if(res.code===500){
					error("生成失败");
					return;
				}
				//console.log(res.outputJson);
				vm.outputJson=res.outputJson;
				// console.log(vm.outputJson["bootstrap-ui"]);
				vm.outputStr=vm.outputJson["plusentity"].trim();
				//console.log(vm.outputJson["bootstrap-ui"]);
				//console.log(vm.outputStr);
				$.outputArea.setValue(vm.outputStr);
				$.outputArea.setSize('auto', 'auto');
				//add to historicalData
				vm.setHistoricalData(res.outputJson.tableName);
				alert("生成成功");
			});
		},
		copy : function (){
			navigator.clipboard.writeText(vm.outputStr.trim()).then(r => {alert("已复制")});
		}
	},
	created: function () {
		//load all templates for selections 加载所有模板供选择
		axios.post("template/all",{
			id:1234
		}).then(function(res){
			//console.log(res.templates);
			vm.templates = JSON.parse(res.templates);
			// console.log(vm.templates);
		});
	},
	updated: function () {
	}
});

